﻿using System;
using System.Security.AccessControl;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ITProf.QuickMailer
{
    internal sealed class SettingsManager
    {
        private const string Path = @"Software\ITProf\QuickMailer\";
        public static readonly SettingsManager Instance = new SettingsManager();

        private SettingsManager()
        {
        }

        public bool AutoClear
        {
            get { return ReadValue("AutoClear", true); }
            set { WriteValue("AutoClear", value); }
        }

        public bool SendToMail0
        {
            get { return ReadValue<bool>("SendToMail0"); }
            set { WriteValue("SendToMail0", value); }
        }

        public bool SendToMail1
        {
            get { return ReadValue<bool>("SendToMail1"); }
            set { WriteValue("SendToMail1", value); }
        }

        public double MainWindowHeight
        {
            get { return ReadValue<double>("MainWindowHeight"); }
            set { WriteValue("MainWindowHeight", value); }
        }

        public double MainWindowWidth
        {
            get { return ReadValue<double>("MainWindowWidth"); }
            set { WriteValue("MainWindowWidth", value); }
        }

        public string Mail0
        {
            get { return ReadValue<string>("Mail0"); }
            set { WriteValue("Mail0", value); }
        }

        public string Mail1
        {
            get { return ReadValue<string>("Mail1"); }
            set { WriteValue("Mail1", value); }
        }

        public string Mail2
        {
            get { return ReadValue<string>("Mail2"); }
            set { WriteValue("Mail2", value); }
        }

        public string Mail3
        {
            get { return ReadValue<string>("Mail3"); }
            set { WriteValue("Mail3", value); }
        }

        public string Mail4
        {
            get { return ReadValue<string>("Mail4"); }
            set { WriteValue("Mail4", value); }
        }

        public bool SendToMail2
        {
            get { return ReadValue<bool>("SendToMail2"); }
            set { WriteValue("SendToMail2", value); }
        }

        public bool SendToMail3
        {
            get { return ReadValue<bool>("SendToMail3"); }
            set { WriteValue("SendToMail3", value); }
        }

        public bool SendToMail4
        {
            get { return ReadValue<bool>("SendToMail4"); }
            set { WriteValue("SendToMail4", value); }
        }

        private T ReadValue<T>(string name)
        {
            T defaultValue = default(T);
            return ReadValue(name, defaultValue);
        }

        private T ReadValue<T>(string name, T defaultValue)
        {
            try
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(Path, RegistryKeyPermissionCheck.ReadSubTree,
                                                                  RegistryRights.ReadKey);
                if (key == null)
                {
                    key = Registry.CurrentUser.CreateSubKey(Path, RegistryKeyPermissionCheck.ReadSubTree);
                }
                object value = key.GetValue(name, defaultValue);
                if (value == null)
                {
                    return defaultValue;
                }
                return (T) Convert.ChangeType(value, typeof (T));
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error");
                return defaultValue;
            }
        }

        private void WriteValue<T>(string name, T value)
        {
            try
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(Path, RegistryKeyPermissionCheck.ReadWriteSubTree,
                                                                  RegistryRights.WriteKey);
                if (key == null)
                {
                    key = Registry.CurrentUser.CreateSubKey(Path, RegistryKeyPermissionCheck.ReadWriteSubTree);
                }
                key.SetValue(name, value);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error");
            }
        }
    }
}